<?php

namespace admin\controller;
use model\menu;
class menus extends _base
{
    public function __construct()
    {
        parent::__construct();
        $this->assign('m','sys');
        $this->assign('y','menus');
    }

    /**
     * @Notes:列表
     * @Interface lists
     * @author: LZH
     * @Time: 2019/6/18   17:30
     */
    public function lists()
    {
        $aParams = array();
        $aParams['field'] = ['id','pid','level','name','mca','status','order_num'];
        $aParams['order'] = ['order_num','desc'];
        $oData = M('menu')->top($aParams);
        $aData = (new menu)->GetCateList($oData);

        $this->display('menu/menu_lists',['aData'=>$aData]);
    }

    /**
     * @Notes:添加
     * @Interface add
     * @author: LZH
     * @Time: 2019/6/18   17:26
     */
    public function add()
    {
        //表单提交操作
        if (I('act') == 'do') {
            $info = [
                'uniacid'=>uniacid(),
                'name'        =>I('name'),
                'mca'       => I('mca'),
                'icon'       => I('icon'),
                'order_num'       => I('order_num'),
                'pid'         =>I('pid'),
                'status' => I('status'),
                'creation_date' => time(),
            ];
            $level=M('menu')->getData(['field' => 'level', 'where' => ['id' => I('pid')]]);
            if($level){
                $info['level']=$level['level']+1;
            }else{
                $info['level']=0;
            }

            if ( M('menu')->getData(['field'=>['id'],'where'=>['name' => $info['name']]]))
                create_info('该栏目已存在', '', 'error');

            if (!$iId = M('menu')->add($info))
                create_info('栏目添加失败', '', 'error');

            //操作记录待补充
            $this->log_act('添加栏目 ID：' . (int)$iId);
            create_info('添加成功', gWebUrl('menus/lists'), 'success');
        }
        $aParams['order'] = ['order_num','desc'];
        $aParams['field'] = ['id','name','pid'];
        $aParams['where'] = ['level in' =>[0,1]];
        $aDatas = M('menu')->top($aParams);
        $level = (new menu)->GetCateList($aDatas);

        $this->display('menu/menu_add',['level'=>$level]);
    }

    /**
     * @Notes:修改
     * @Interface edit
     * @author: LZH
     * @Time: 2019/6/18   17:31
     */
    public function edit()
    {
        //表单提交操作
        if (I('act') == 'do') {
            $info = array(
                'name'        =>I('name'),
                'mca'       => I('mca'),
                'icon'       => I('icon'),
                'order_num'       => I('order_num'),
                'pid'         =>I('pid'),
                'status' => I('status'),
                'last_update' => time(),
            );
            $level=M('menu')->getData(['field' => 'level', 'where' => ['id' => I('pid')]]);
            if($level){
                $info['level']=$level['level']+1;
            }else{
                $info['level']=0;
            }
            //where条件
            $aParams['where']['id'] = I('id');
            if ( M('menu')->getData(['field'=>['id'],'where'=>['name' => $info['name'],'id !=' =>I('id') ]]))
                create_info('该栏目已存在', '', 'error');

            if (!M('menu')->edit($info,$aParams))
                create_info('修改栏目失败', '', 'error');

            //操作记录待补充
            $this->log_act('修改栏目 ID：' . (int)I('id'));
            create_info('成功修改栏目信息',gWebUrl('menus/lists'),'success');
        }

        $iRuleId = i('id');
        $aParams['where'] = array('id' => $iRuleId);
        if (!$aData = M('menu')->getData($aParams))
            message('该数据不存在','','error');

        $aParams1['field'] =['id','name','pid','level'];
        $aParams1['where'] = ['level in' =>[0,1]];
        $aDatas = M('menu')->top($aParams1);
        $level = (new menu)->GetCateList($aDatas);
        $this->display('menu/menu_edit',['aData'=>$aData,'level'=>$level]);
    }

    /**
     * @Notes:修改状态
     * @Interface change
     * @author: LZH
     * @Time: 2019/6/18   17:34
     */
    public function change()
    {
        $iId = I('id');
        $iStatus = I('status');
        $aParams['where'] = array('id' => $iId);
        if (!$aData = M('menu')->getData($aParams))
            create_info('该栏目不存在', '', 'error');
        if (!M('menu')->edit(['status' => $iStatus], $aParams))
            create_info('修改状态失败', '', 'error');

        $this->log_act('修改栏目状态 ID：' . (int)$iId);
        create_info('修改状态成功', gWebUrl('menus/lists'), 'success');
    }

    /**
     * @Notes:删除
     * @Interface del
     * @author: LZH
     * @Time: 2019/6/18   17:33
     */
    public function del()
    {
        $iId = I('id');
        $aParams['where'] = array('id' => $iId);
        if (!$aData = M('menu')->getData($aParams))
            create_info('该栏目不存在', '', 'error');
        if (!M('menu')->edit(['is_delete'=>2],$aParams))
            create_info('删除失败', '', 'error');

        $this->log_act('删除栏目 ID：' . (int)$iId);
        create_info('删除成功', gWebUrl('menus/lists'), 'success');
    }

    public function del_all(){
        $iId = I('ids');
        $aParams['where'] =['id in'=>$iId];
        if (!M('menu')->edit(['is_delete'=>2],$aParams))
            create_info('删除失败', '', 'error');

        $this->log_act('删除栏目 ID：' . $iId);
        create_info('删除成功', gWebUrl('menus/lists'), 'success');
    }


}

